﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wd_6_trayicon
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            AddTrayIcon();
            OpenNotepad();
        }

        NotifyIcon trayIcon = null;     // 托盘
        private void AddTrayIcon()
        {
            if (trayIcon != null)
            {
                return;
            }
            trayIcon = new NotifyIcon();
            this.trayIcon.BalloonTipText = "ball";
            this.trayIcon.Icon = new System.Drawing.Icon("./alpha_orange_l.ico");   //ico要在bin目录下
            this.trayIcon.Text = "test123";
            trayIcon.Visible = true;
            trayIcon.ShowBalloonTip(1000);

            System.Windows.Forms.ContextMenu menu = new System.Windows.Forms.ContextMenu();

            System.Windows.Forms.MenuItem closeItem = new System.Windows.Forms.MenuItem();
            closeItem.Text = "关闭";
            //closeItem.Click += new EventHandler(delegate { this.Shutdown(); });   //方式1
            //closeItem.Click += (o, e1) => { System.Windows.Application.Current.Shutdown(); };  // 方式2 -- https://blog.csdn.net/weixin_53370274/article/details/122825780
            closeItem.Click += (o, e1) => { Environment.Exit(-1); };  // 方式3 -- https://blog.csdn.net/june905206961/article/details/120343871

            System.Windows.Forms.MenuItem addItem = new System.Windows.Forms.MenuItem();
            addItem.Text = "Menu";

            //menu.MenuItems.Add(addItem);  // 多余的项
            menu.MenuItems.Add(closeItem);  //关闭

            trayIcon.ContextMenu = menu;
        }


        private void RemoveTrayIcon()
        {
            if (trayIcon != null)
            {
                trayIcon.Visible = false;
                trayIcon.Dispose();
                trayIcon = null;
            }
            Thread.Sleep(1);
        }

        private void OpenNotepad()
        {
            Process.Start("notepad.exe");
        }

        private void KillNotepad()
        {
            Process p = new Process();

            p.StartInfo.FileName = "taskkill";
            p.StartInfo.Arguments = "/f /im notepad.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;

            p.Start();
            string output = string.Empty;
            output = p.StandardOutput.ReadToEnd();
            Thread.Sleep(1);
            p.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = false;
            KillNotepad();
            RemoveTrayIcon();
            Environment.Exit(-2);
        }
    }
}
